var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    explainShow: false,
    unpackingShow: false,
    scrollViewWidth: 0,
    scrollLeft: 0,
    activeIndex: 0,
    scrollIntoView: '',
    allDataList:{},
    inShow: false,
    info: false,
    imgPath: app.globalData.imgPath,
  },
  onShow: function () {
    this.setData({
      info: true
    })
  },
  onHide: function () {
    this.setData({
      info: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.createSelectorQuery().select('#new-blind-box').boundingClientRect((rect) => {
      this.setData({
        scrollViewWidth: rect.width
      })
    }).exec()
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split('&');

        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split('=');
          if (chil_arr[0] == 'id') {
            this.getData(chil_arr[1].split(',')[0]);
          }
          if(chil_arr[0] == 'mid'){
            app._setMid(chil_arr[1]);
          }
        }
      }
    }else {
      this.getData(options.id);
    }
  },
 
  clickExplain() {
    this.setData({ explainShow: true });
  },

  clickClose() {
    this.setData({ explainShow: false });
  },
  // 立即开箱 
  clickUnpacking() {
    if(this.data.allDataList.list[this.data.activeIndex].show_status !== 1) { //  0未开始，1进行中，2已结束
      return
    }
    this.setData({ unpackingShow: true });
  },
  // 取消
  cancalEvent(e) {
    this.setData({ unpackingShow: false });
  },
  // 选择等级
  chooseLevel(e) {
    let offsetLeft = e.currentTarget.offsetLeft 
    this.setData({
        scrollLeft: (offsetLeft - this.data.scrollViewWidth / 2) + 50,
        activeIndex : e.currentTarget.dataset.index
    })
  },
  // 上一张
  prevImg() {
    if(!this.data.activeIndex) { return }

    let index = JSON.parse(JSON.stringify(this.data.activeIndex));
    this.setData({
      scrollIntoView: `item${ index - 2 >= 0 ? index - 2 : 0}`,
      activeIndex : this.data.activeIndex <= 0 ? 0 : this.data.activeIndex - 1
    })
    if(this.data.activeIndex == 0) {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  // 下一张
  nextImg() {
    if(this.data.allDataList.list.length - 1 <= this.data.activeIndex) {
      return
    }
    this.setData({
      scrollIntoView: `item${this.data.activeIndex}`,
      activeIndex :  this.data.activeIndex + 1
    })
  },
  // 获取数据
  getData(id) {
    app._getNetWork({
      url: app.getNetAddresss('plugin.new-blind-box.frontend.index.index'),
      data: {},
      success: ({data}) => {
        if(data.result) {
          let cindex = id ? data.data.list.findIndex(item => item.id == id) : 0;
          this.setData({
            allDataList: data.data,
            scrollIntoView: cindex - 1 <= 0 ? '' : `item${cindex - 1}`,
            activeIndex: cindex,
            inShow: true
          })
        }else {
          wx.showToast({
            icon: 'none',
            title: data.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    })
  }
})